package com.dashuaibran.joker.dao.mysqlimpl;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.dashuaibran.joker.dao.IUserDao;
import com.dashuaibran.joker.domain.User;

public class UserDao implements IUserDao {

	private QueryRunner runner;

	public void setRunner(QueryRunner runner) {
		this.runner = runner;
	}

	public void find() {
		// TODO Auto-generated method stub
		System.out.println("find");
	}

	public User find(Integer id) {
		// TODO Auto-generated method stub
		try {
			return runner.query("select * from user where id=?", new BeanHandler<User>(User.class), id);
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}

	public List<User> findAll() {
		// TODO Auto-generated method stub
		try {
			return runner.query("select * from user", new BeanListHandler<User>(User.class));
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}

	public Integer update(User user) {
		// TODO Auto-generated method stub
		try {
			return runner.update("update user set name=?,email=?,age=? where id=?", user.getName(), user.getEmail(),
					user.getAge(), user.getId());
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}

	public Integer add(User user) {
		// TODO Auto-generated method stub
		try {
			return runner.update("insert into user(name,email,age) value(?,?,?)", user.getName(), user.getEmail(),
					user.getAge());
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}

	public Integer delete(Integer id) {
		// TODO Auto-generated method stub
		try {
			return runner.update("delete from user where id=?", id);
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}

}
